module.exports = function(Crime) {

	Crime.getNearlyTen = function (lat, lng, cb) {
	  var option = {
	    where: {
	      and: [
	        {JD: {between: [lng - 0.01, lng + 0.01]}},
	        {WD: {between: [lat - 0.01, lat + 0.01]}}
	      ]
	    }
	  };
	  var result = [];
	  Crime.find(option, function (err, data) {
	    for(var i = 0; i < data.length; i++) {
	      result.push({
	        type: 'crime',
	        msg: data[i].ADDR,
	        addr: data[i].ADDR,
	        lat: data[i].WD - 0,
	        lng: data[i].JD - 0,
	        distance: Math.floor(Math.abs(data[i].WD - lat) * 110000 
	          + Math.abs(data[i].JD - lng) * 94000)
	      })
	    }
	    cb(null, result);
	  })
	};

	Crime.remoteMethod(
	  'getNearlyTen', {
	    http: {path: '/getNearlyTen', verb: 'post'},
	    accepts: [
	      {arg: 'lat', type: 'number', required: 'true'},
	      {arg: 'lng', type: 'number', required: 'true'}
	    ],
	    returns: {type: 'array', root: true}
	  }
	);

	Crime.searchData = function (searchText, lat, lng, cb) {
	  var option = {
	    where: {
	      or: [
	        {ADDR: {like: '%' + searchText + '%'}},
	      ]
	    }
	  };
	  var result = [];
	  Crime.find(option, function (err, data) {
	    for(var i = 0; i < data.length; i++) {
	      result.push({
	        type: 'crime',
	        msg: data[i].ADDR,
	        addr: data[i].ADDR,
	        lat: data[i].WD - 0,
	        lng: data[i].JD - 0,
	        distance: Math.floor(Math.abs(data[i].WD - lat) * 110000 
	          + Math.abs(data[i].JD - lng) * 94000)
	      })
	    }
	    cb(null, result);
	  })
	};

	Crime.remoteMethod(
	  'searchData', {
	    http: {path: '/searchData', verb: 'post'},
	    accepts: [
	      {arg: 'searchText', type: 'string', required: 'true'},
	      {arg: 'lat', type: 'number', required: 'true'},
	      {arg: 'lng', type: 'number', required: 'true'}
	    ],
	    returns: {type: 'array', root: true}
	  }
	);

};
